<?php
class Axe extends CActiveRecord {
	
	public static function model($className=__CLASS__)	{
		return parent::model($className);
	}
	public function tableName() {
		return 'axe';
	}
	public function getFullname()  {
      if ($this->parent && ($this->parent != $this->id)) {
			$parentnode = Axe::model()->findByPk($this->parent);
			return $parentnode->fullname . ',' . $this->name;
		}
		else {
			return $this->name;
		}
   }
	public function getFullpath() {
		
      if ($this->parent && ($this->parent != $this->id)) {
			$parentnode = Axe::model()->findByPk($this->parent);
			return $parentnode->fullpath . ',' . $this->id;
		}
		else {
			return $this->id;
		}				
	}
	public function behaviors() {
		return array(
			'NestedSetBehavior'=>array(
            'class'			  =>'ext.behaviors.NestedSetBehavior',
				'hasManyRoots'   => true,
            'leftAttribute'  => 'left',
            'rightAttribute' => 'right',
            'levelAttribute' => 'level',
				'rootAttribute'  => 'root'
			)
		);
	}
	public function relations() {
		return array(
		);
	}
	public function beforeSave() {
		if ($this->isNewRecord)
			$this->created_date = new CDbExpression('NOW()');
		else
			$this->modified_date = new CDbExpression('NOW()');
		return parent::beforeSave();
	}
	public function _save() {
		if ($this->parent) {
			$parent = Axe::model()->findByPk($this->parent);
			if ($parent) {
				try {
					$this->appendTo($parent);
					$this->saveNode();
					return $this;
				}
				catch (Exception $e) {
					return null;
				}
			}
			else {
				return null;
			}
		}
		else {
			try {
				$this->saveNode();
				return $this;
			}
			catch (Exception $e) {
				return null;
			}
		}
	}
	public function _update() {
		if ($this->parent) {
			$parent = Axe::model()->findByPk($this->parent);
			if ($parent) {
				try {
					$this->appendTo($parent);
					$this->saveNode();
					return $this;
				}
				catch (Exception $e) {
					return null;
				}
			}
			else {
				return null;
			}
		}
		else {
			try {
				$this->moveAsRoot();
				$this->saveNode();
				return $this;
			}
			catch (Exception $e) {
				return null;
			}
		}	
	}
	public function _list($query = null, $order=null) {
		$criteria = new CDbCriteria;
		try {
			$criteria->addCondition($query);
			$criteria->order = $order;
			$results = $this->findAll($criteria);
			return $results;
		}
		catch (Exception $e) {
			return null;
		}
	}
	public function _delete() {
		try {
			$this->deleteNode();
			return true;
		}
		catch (Exception $e) {
			return null;
		}
	}
	public function _view($id) {
		$connection = Yii::app()->db;
		$command = $connection->createCommand($sql);
		$level = $command->query("SELECT max(level) FROM axe;");
		if ($id) {
			$node = $this->findByPk($id);
			if ($node) {
				
				$result = array();
				$result['id'] = $node->id;
				$result['name'] = $node->name;
				$result['fullname'] = $node->fullname;
				$fullpath = explode(',', $this->fullpath);
				$fullpath = array_pad($fullpath, $level - sizeof($fullpath), '0');
				$result['fullpath'] = implode(',', $fullpath);
				$result['left'] = $node->left;
				$result['right'] = $node->right;
				$result['level'] = $node->level;
				$result['root'] = $node->root;
				$result['parent'] = $node->parent;
				$result['children'] = $node->children()->findAll();
				return $result;
			}
			else {
				return null;
			}
		}
		else {
			return null;
		}
	}
}